<?php
class UserController extends Exts_Controller_User
{
		

    public function registryAction()
	{
		$form = new Application_Form_Registry();
		
		if( $this->getRequest()->isPost() ) {
			if( $form->isValid( $this->_getAllParams() ) ) {

				if( $form->getValue( 'username' ) !=  $form->getValue( 'username_2' )) {
					$this->_helper->getHelper('message')->addError('The email not the same');
					$this->view->form = $form;
					return $this->render();
				}
				
				if( $form->getValue( 'password' ) !=  $form->getValue( 'password_2' )) {
					$this->_helper->getHelper('message')->addError('The password not the same');
					$this->view->form = $form;
					return $this->render();
				}
				
				$user = new Application_Model_User();
				if( $user->exist( $form->getValue('username') ) ){
					$this->_helper->getHelper('message')->addError('The username exist. Use other.');
					$this->view->form = $form;
					return $this->render();
				}
				try{ 
					$userId = $user->save( $form->getValues() );
				} catch( Exception $e ) {
					$this->_helper->getHelper('message')->addError('An occurred error try again later.');
					$this->view->form = $form;
					return $this->render();				
				}
				
				$this->_helper->getHelper('message')->addSuccess('Registry successfull.');
				$user = new Application_Model_User();
				if( $user->login( $form->getValue(  'username' ), $form->getValue(  'password' ) )) {
					$row = $user->exist( $form->getValue('username' ) );
					$u = new stdClass();
					$u->username = $row->username;
					$u->id = $row->user_id;
					$this->_session->user = $u;
					return $this->_redirect( '/' );
				}
				
			} else {
				$this->_helper->getHelper('message')->addError('The formulary content error.');
			}
		}
		$this->view->form = $form;
	}

	public function permissiondeniedAction()
	{
	}
	
}